﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using ExcelCOM = Microsoft.Office.Interop.Excel;
using RCA1._0.DAL_WS;
namespace RCA1._0
{
    public partial class TrangChu : Form
    {
        public TrangChu()
        {
            InitializeComponent();
        }
        
        private DAL_WSSoapClient wsDAL = new DAL_WSSoapClient();
        private List<CONGTYDTO> dsCongTy;
        private List<THANHVIENDTO> dsThanhVien;

        private List<LOAICONGTYDTO> dsLoaiCongTy;

        private void TrangChu_Load(object sender, EventArgs e)
        {
            dsLoaiCongTy=new List<LOAICONGTYDTO>(wsDAL.GetDanhSachLoaiCongTy());
            dtvcmbMaLoaiCongTy.DataSource = dsLoaiCongTy;
            dtvcmbMaLoaiCongTy.DisplayMember = "TenLoaiCongTy";
            dtvcmbMaLoaiCongTy.ValueMember = "IDLoaiCongTy";
            
            //Thể hiện danh sách công ty
            TheHienDanhSachCongTy();
            
        }

        void TheHienDanhSachCongTy()
        {
            //Lấy danh sách công ty mới
            dsCongTy = new List<CONGTYDTO>(wsDAL.GetDanhSachCongTy());

            //Thể hiện danh sách công ty ra Bảng
            dtgvKQTimKiem.DataSource = dsCongTy;
        }

        void TheHienDanhSachThanhVien()
        {
            //Lấy danh sách thành viên mới
            dsThanhVien = new List<THANHVIENDTO>(wsDAL.GetDanhSachThanhVien());

            //Thể hiện danh sách thành viên ra Bảng
            //dtgvKQTimKiem.DataSource = dsThanhVien;
        }


        #region "Tìm kiếm"
        private void btnTimKiemTen_Click(object sender, EventArgs e)
        {
            String tenCongTy = txtTimKiemTen.Text;
            dtgvKQTimKiem.DataSource = wsDAL.TimKiemTheoTenCongTy(tenCongTy) ;
        }

        private void btnTimKiemTenVT_Click(object sender, EventArgs e)
        {
            String tenVietTat = txtTimKiemTenVT.Text;
            dtgvKQTimKiem.DataSource = wsDAL.TimKiemTheoTenVietTat(tenVietTat);
        }
                

        //Lưu xảy ra sự thay đổi Xóa, Sửa đối tượng Công Ty trong Form Công Ty
        public static int xetThayDoi_CongTy;
        
        private void dtgvKQTimKiem_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            xetThayDoi_CongTy = 0;
            //Lấy đối tượng Công Ty được chọn            
            CONGTYDTO congTy_frmCongTy = (CONGTYDTO)dsCongTy[e.RowIndex];

            //Gọi Form Công ty
            CongTy frm = new CongTy();
            frm.congTy = congTy_frmCongTy;
            frm.ShowDialog();

            //Nếu xảy ra sự thay đổi
            if (xetThayDoi_CongTy == 1)
            {
                //Thể hiện lại danh sách công ty
                TheHienDanhSachCongTy();
            }
            
        }

        String TimTenLoaiCongTy(int MaLoaiCongTy)
        {
            foreach (LOAICONGTYDTO loaiCongTy in dsLoaiCongTy)
                if (MaLoaiCongTy == loaiCongTy.IDLoaiCongTy)
                    return loaiCongTy.TenLoaiCongTy;
            return null;
        }

        String TimTenCongTy(int MaCongTy)
        {
            foreach (CONGTYDTO tenCongTy in dsCongTy)
                if (MaCongTy == tenCongTy.IDCongTy)
                    return tenCongTy.TenCongTy;
            return null;
        }
        
        #endregion


        #region "Menu nối liên kết"
        private void DanhSachCongTyToolStripMenuItem_Click(object sender, EventArgs e)
        {

            ThemCongTy frm = new ThemCongTy();
            frm.ShowDialog();
            TheHienDanhSachCongTy();

        }

        private void ThemCongTyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ThemCongTy frm = new ThemCongTy();
            frm.ShowDialog();
            TheHienDanhSachCongTy();
        }

        private void DanhSachThanhVienToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ThemThanhVien frm = new ThemThanhVien();
            frm.ShowDialog();
        }

        private void ThemThanhVienToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ThemThanhVien frm = new ThemThanhVien();
            frm.ShowDialog();
        }

        private void SoanDieuLeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SoanDieuLe frm = new SoanDieuLe();
            frm.ShowDialog();
        }

        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About frm = new About();
            frm.ShowDialog();
        }

        private void ThoatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        
        #region "Xuất ra file"
        private void XuatDSCTyRaFileExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialogCTy.Filter = "Excel Workbook (*.xls)|*.xls";
            saveFileDialogCTy.ShowDialog();
            
            
            
            ExcelCOM.Application exApp = new ExcelCOM.Application();
            ExcelCOM.Workbook exBook = exApp.Workbooks.Add(ExcelCOM.XlWBATemplate.xlWBATWorksheet);
            ExcelCOM.Worksheet exSheet = (ExcelCOM.Worksheet)exBook.ActiveSheet;


            exSheet.get_Range("A1", Type.Missing).Value2 = "Tên Công Ty";
            exSheet.get_Range("B1", Type.Missing).Value2 = "Tên Giao Dịch Công Ty";
            exSheet.get_Range("C1", Type.Missing).Value2 = "Tên Viết Tắt";
            exSheet.get_Range("D1", Type.Missing).Value2 = "Vốn điều lệ số";
            exSheet.get_Range("E1", Type.Missing).Value2 = "Vốn điều lệ bằng chữ";
            exSheet.get_Range("F1", Type.Missing).Value2 = "Triệu vốn";
            exSheet.get_Range("G1", Type.Missing).Value2 = "Địa chỉ trụ sở chính";
            exSheet.get_Range("H1", Type.Missing).Value2 = "Ngành nghề kinh doanh";
            exSheet.get_Range("I1", Type.Missing).Value2 = "Loại hình kinh doanh";
            exSheet.get_Range("J1", Type.Missing).Value2 = "Điện thoại";
            exSheet.get_Range("K1", Type.Missing).Value2 = "Fax";
            exSheet.get_Range("L1", Type.Missing).Value2 = "Email";
            exSheet.get_Range("M1", Type.Missing).Value2 = "Website";

            for (int i = 0; i < dsCongTy.Count;i++ )
            {
                int j = i + 2;
                CONGTYDTO congTy=dsCongTy[i];
                exSheet.get_Range("A" + j.ToString(), Type.Missing).Value2 = congTy.TenCongTy;
                exSheet.get_Range("B" + j.ToString(), Type.Missing).Value2 = congTy.TenGiaoDichCongTy;
                exSheet.get_Range("C" + j.ToString(), Type.Missing).Value2 = congTy.TenVietTat;
                exSheet.get_Range("D" + j.ToString(), Type.Missing).Value2 = congTy.VonDieuLeSo;
                exSheet.get_Range("E" + j.ToString(), Type.Missing).Value2 = congTy.VonDieuLeBangChu;
                exSheet.get_Range("F" + j.ToString(), Type.Missing).Value2 = congTy.MenhGiaCoPhan;
                
                exSheet.get_Range("H" + j.ToString(), Type.Missing).Value2 = congTy.NganhNgheKinhDoanhChinh;
                exSheet.get_Range("I" + j.ToString(), Type.Missing).Value2 = TimTenLoaiCongTy(congTy.MaLoaiCongTy);
                
                exSheet.get_Range("L" + j.ToString(), Type.Missing).Value2 = congTy.Email;
                exSheet.get_Range("M" + j.ToString(), Type.Missing).Value2 = congTy.Website;
            }

            String path = saveFileDialogCTy.FileName;
            exApp.Visible = false;
            exBook.SaveAs(path, ExcelCOM.XlFileFormat.xlWorkbookNormal,
                null, null, false, false,
                ExcelCOM.XlSaveAsAccessMode.xlExclusive,
                false, false, false, false, false);

            exBook.Close(false, false, false);
            exApp.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp);

            MessageBox.Show("Đã export ra file " + path);

        }
        

        private void XuatDSThVienRaFileExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialogThVien.Filter = "Excel Workbook (*.xls)|*.xls";
            saveFileDialogThVien.ShowDialog();

            TheHienDanhSachThanhVien();



            ExcelCOM.Application exApp = new ExcelCOM.Application();
            ExcelCOM.Workbook exBook = exApp.Workbooks.Add(ExcelCOM.XlWBATemplate.xlWBATWorksheet);
            ExcelCOM.Worksheet exSheet = (ExcelCOM.Worksheet)exBook.ActiveSheet;


            exSheet.get_Range("A1", Type.Missing).Value2 = "Tên thành viên viết thường";
            exSheet.get_Range("B1", Type.Missing).Value2 = "Tên tên thành viên viết hoa";
            exSheet.get_Range("C1", Type.Missing).Value2 = "Tên công ty";
            exSheet.get_Range("D1", Type.Missing).Value2 = "Ngày tháng năm sinh";
            exSheet.get_Range("E1", Type.Missing).Value2 = "Giới tính";
            exSheet.get_Range("F1", Type.Missing).Value2 = "Quốc tịch";
            exSheet.get_Range("G1", Type.Missing).Value2 = "Dân tộc";
            exSheet.get_Range("H1", Type.Missing).Value2 = "Địa chị thường trú";
            exSheet.get_Range("I1", Type.Missing).Value2 = "Nơi ở hiện tại";
            exSheet.get_Range("J1", Type.Missing).Value2 = "Số CMND/ ĐKKD";
            exSheet.get_Range("K1", Type.Missing).Value2 = "Ngày cấp";
            exSheet.get_Range("L1", Type.Missing).Value2 = "Nơi cấp";
            exSheet.get_Range("M1", Type.Missing).Value2 = "Tổng giá trị góp vốn";
            exSheet.get_Range("N1", Type.Missing).Value2 = "Sở hữu vốn";
            exSheet.get_Range("O1", Type.Missing).Value2 = "Phần trăm góp vốn";
            exSheet.get_Range("P1", Type.Missing).Value2 = "Thời điểm góp vốn";
            

            for (int i = 0; i < dsThanhVien.Count; i++)
            {
                int j = i + 2;
                THANHVIENDTO thanhVien = dsThanhVien[i];
                exSheet.get_Range("A" + j.ToString(), Type.Missing).Value2 = thanhVien.TenThanhVienThuong;
                exSheet.get_Range("B" + j.ToString(), Type.Missing).Value2 = thanhVien.TenThanhVienInHoa;
                exSheet.get_Range("C" + j.ToString(), Type.Missing).Value2 = TimTenCongTy(thanhVien.MaCongTy);
                exSheet.get_Range("D" + j.ToString(), Type.Missing).Value2 = thanhVien.NTNS;
                exSheet.get_Range("E" + j.ToString(), Type.Missing).Value2 = thanhVien.GioiTinh;
                exSheet.get_Range("F" + j.ToString(), Type.Missing).Value2 = thanhVien.QuocTich;
                exSheet.get_Range("G" + j.ToString(), Type.Missing).Value2 = thanhVien.DanToc;
                exSheet.get_Range("H" + j.ToString(), Type.Missing).Value2 = thanhVien.DiaChiThuongTru;
                exSheet.get_Range("I" + j.ToString(), Type.Missing).Value2 = thanhVien.NoiOHienTai;
                exSheet.get_Range("J" + j.ToString(), Type.Missing).Value2 = thanhVien.SoCMND;
                exSheet.get_Range("K" + j.ToString(), Type.Missing).Value2 = thanhVien.NgayCapCMND;
                exSheet.get_Range("L" + j.ToString(), Type.Missing).Value2 = thanhVien.NoiCapCMND;
                exSheet.get_Range("M" + j.ToString(), Type.Missing).Value2 = thanhVien.TongGiaTriGopVon;
                exSheet.get_Range("N" + j.ToString(), Type.Missing).Value2 = thanhVien.SoHuuVon;
                exSheet.get_Range("O" + j.ToString(), Type.Missing).Value2 = thanhVien.PhanTramGopVon;
                exSheet.get_Range("P" + j.ToString(), Type.Missing).Value2 = thanhVien.ThoiDiemGopVon;
            }

            String path = saveFileDialogThVien.FileName;
            exApp.Visible = false;
            exBook.SaveAs(path, ExcelCOM.XlFileFormat.xlWorkbookNormal,
                null, null, false, false,
                ExcelCOM.XlSaveAsAccessMode.xlExclusive,
                false, false, false, false, false);

            exBook.Close(false, false, false);
            exApp.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp);

            MessageBox.Show("Đã export ra file " + path);
        }

        #endregion


        private void dtgvKQTimKiem_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }


    }
}
